/*
	eval XFCN
	17 Mar 1989 Greg Jorgensen, Mac DTS
	й Apple Computer Inc., 1989
	
	usage: eval(expression)
	
	returns: empty if expression is valid, error msg otherwise
	
	error returns:
	  param error    -- wrong number of parameters
	  too long       -- expression longer than 255 characters
	  eval error     -- not a valid expression
	
	This XFCN is intended to evaluate expressions that would cause
	a Hypercard error if they failed. For example,
	
	  "x + 0"  -- fails if x is non-numeric
	  "script of cd btn 3"  -- fails if there is no cd btn 3
	  
	It can be used to test for the existence of objects, whether
	an expression is numeric, etc.
	
	NOTE: The expression to be evaluated must be < 256 characters long.
	
*/

#include <MacTypes.h>
#include <SetUpA4.h>
#include <strings.h>
#include <HyperXCmd.h>

#define	NIL	0L


/* Hypercard parameter block:

	typedef struct XCmdBlock {
		short	paramCount;     
	    Handle  params[16];
	    Handle  returnValue;      
	    Boolean	passFlag; 
	      
	    void	(*entryPoint)();
	    short	request;  
	    short	result;  
	    long	inArgs[8];
	    long	outArgs[4];
	   } XCmdBlock, *XCmdBlockPtr;
*/

/* еее */

pascal void main(paramPtr)
	XCmdBlockPtr	paramPtr;
{
	int		k;
	char	*s;
	Handle	hResult;
	
	RememberA0();					/* A0 points to this procedure */
	SetUpA4();						/* point A4 at this proc for literals */
	
	s = "";
	
	if (paramPtr->paramCount == 1) {
		HLock(paramPtr->params[0]);
		k = strlen( *(paramPtr->params[0]) );
		if (k > 255)
			s = "too long";
		else if (k == 0)
			s = "";
		else {
			CtoPstr( *(paramPtr->params[0]) );
			hResult = EvalExpr(paramPtr, *(paramPtr->params[0]) );
			DisposHandle(hResult);
			PtoCstr( *(paramPtr->params[0]) );
			if (paramPtr->result == 0)
				s = "";
			else
				s = "eval error";
		}
	}
	else
		s = "param error"; 
			
		
	HUnlock(paramPtr->params[0]);
	
	if ( strlen(s) ) {
		paramPtr->returnValue = NewHandle( (long)strlen(s) );
		HLock(paramPtr->returnValue);
		strcpy(*(paramPtr->returnValue), s);
		HUnlock(paramPtr->returnValue);
	}
	else
		paramPtr->returnValue = NIL;
		
	RestoreA4();					/* point A4 back to where it was */
}

#include "XCmdGlue.inc.c"
